package com.baijiaoxi.common.model.base.service.base;

import com.baijiaoxi.common.model.base.mapper.CoreMapper;
import com.baijiaoxi.common.model.base.model.CoreModel;
import com.baijiaoxi.common.model.base.service.interceptor.BaseInterceptor;
import com.baijiaoxi.common.model.base.service.interceptor.DeleteInterceptor;

import java.io.Serializable;
import java.util.List;

public interface DeleteService<E extends CoreModel> extends BaseService<E>, DeleteInterceptor<E>, BaseInterceptor<E> {


    /**
     * 批量删除
     */
    default void del(E entity) {
        CoreMapper<E> mapper = getMapper();
        List<E> eList;
        if (isInterceptDelBatchAsSingle()) {
            eList = mapper.getList(entity);
            eList.parallelStream().forEach(e -> beforeDel(e));
            mapper.del(entity);
            eList.parallelStream().forEach(e -> {
                afterDel(e);
                afterChange(ChangeType.delBatchAsSingle, e);
                    }
            );
        } else {
            beforeDel(entity);
            mapper.del(entity);
            afterDel(entity);
            afterChange(ChangeType.delBatch, entity);
        }
    }


    /**
     * 批量删除
     */
    default void delById(Serializable entityId) {
        CoreMapper<E> mapper = getMapper();
        E entity = mapper.selectById(entityId);
        if (entity != null) {
            beforeDel(entity);
            mapper.deleteById(entityId);
            afterDel(entity);
            afterChange(ChangeType.delById, entity);
        }

    }


}
